package com.jiajia.study.chapter03.method;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * <h1>两阶段终止模式的运用</h1>
 * @author zjiajia
 * @date 2023/11/2 11:13
 */
@Slf4j(topic = "c.monitor")
public class Monitor  {

    private Thread thread;


    public void start(){
        thread = new Thread(()->{
            while(true){
                // 正常运行的时候被打断
                if(thread.isInterrupted()){
                    log.debug("料理后事.........");
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1L);
                } catch (InterruptedException e) {
                    // 修改 interrupt 的状态
                    thread.interrupt();
                    e.printStackTrace();
                }
                // todo : 业务逻辑处理
                log.debug("进行监控>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            }
        },"monitor");
        thread.start();
    }

    public void stop(){
        thread.interrupt();
    }
}
